package com.yonyou.cyxdms.sysAuth.service.controller.manage.position;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.CommonTreeDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.orgposition.OrgPositionPcDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.OrgPositionPO;
import com.yonyou.cyxdms.sysAuth.service.service.orgposition.OrgPositionService;
import com.yonyou.cyxdms.sysAuth.service.service.permission.PermissionCtrlService;
import com.yonyou.cyxdms.sysAuth.service.service.permission.PermissionMenuActionService;
import com.yonyou.cyxdms.sysAuth.service.service.permission.PermissionMenuRangeService;
import com.yonyou.cyxdms.sysAuth.service.service.permission.PermissionService;
import com.yonyou.cyxdms.sysAuth.service.service.role.RoleMenuActionService;
import com.yonyou.cyxdms.sysAuth.service.service.role.RoleMenuRangeService;
import com.yonyou.cyxdms.sysAuth.service.service.role.RoleService;
import com.yonyou.cyxdms.sysAuth.service.vo.orgposition.OrgPositionDepartVO;
import com.yonyou.cyxdms.sysAuth.service.vo.orgposition.OrgPositionPageVO;
import com.yonyou.cyxdms.sysAuth.service.vo.orgposition.OrgPositionVO;
import com.yonyou.cyxdms.sysAuth.service.vo.role.RoleVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职位管理
 *
 * @author dengqiang
 * @since 2019/7/10
 */
@Api(value = "职位管理", tags = {"职位管理"})
@RestController
@RequestMapping("/basedata/orgPosition")
public class PositionManageController extends BaseController {

    @Autowired
    private OrgPositionService orgPositionService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuActionService roleMenuActionService;

    @Autowired
    private RoleMenuRangeService roleMenuRangeService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private PermissionCtrlService permissionCtrlService;
    @Autowired
    private PermissionMenuActionService permissionMenuActionService;
    @Autowired
    private PermissionMenuRangeService permissionMenuRangeService;

    // 定义日志接口
    private static final Logger logger = LoggerFactory.getLogger(PositionManageController.class);


    /**
     * 获取所有职务信息
     *
     * @author dengqiang
     * @since 2019/7/10
     */
    @ApiOperation(value="获取所有职务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping
    public IPage<OrgPositionPageVO> getorgPosition(@ApiParam(name = "pageNum", value = "当前页") @RequestParam("pageNum") Long current
            , @ApiParam(name = "limit", value = "分页大小") @RequestParam("limit") Long size,@RequestParam Map<String, Object> param){
        Page<OrgPositionPageVO> page = new Page(current, size);
        return orgPositionService.queryorgpositiondms(page, param);
    }


    /**
     * 根据职务id获取所挂角色
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @GetMapping(value = "/role/{id}")
    @ResponseBody
    public List<RoleVO> getOrgPositionRoleById(@PathVariable(value = "id") Long id){
        return orgPositionService.getOrgPositionRoleById(id);
    }


    /**
     * 根据职务id获取所挂组织
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @GetMapping(value = "/depart/{id}")
    @ResponseBody
    public List<OrgPositionDepartVO> getOrgPositionDepartById(@PathVariable(value = "id") Long id){
        return orgPositionService.getOrgPositionDepartById(id);
    }


    /**
     * 新增职务
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @PostMapping
    public Integer addorgPosition(@RequestBody OrgPositionPcDTO ptdto){
       return orgPositionService.addOrgPosition(ptdto);
    }


    /**
     * 根据id获取职位信息
     *
     * @author dengqiang
     * @since 2019/7/11
     */
    @GetMapping(value = "/{id}")
    @ResponseBody
    public OrgPositionVO getOrgPositionById(@PathVariable(value = "id") Long id){
        OrgPositionPO ptPo=orgPositionService.getOrgPositionById(id);

        OrgPositionVO orgPositionVO = new OrgPositionVO();
        BeanUtils.copyProperties(ptPo,orgPositionVO);

        String roleIdsStr=orgPositionService.findRoleIdsByPositionId(id);
        orgPositionVO.setRoleids(roleIdsStr);
        return orgPositionVO;
    }

    /**
     * @author     ：quankk
     * @date       ：2019/7/24 20:23
     * @description：获取所有职务信息
     */
    @RequestMapping(value = "/type",method=RequestMethod.GET)
    @ApiOperation(value = "获取所有职务信息")
    public IPage<OrgPositionPageVO> getorgPositionType(@ApiParam(name = "pageNum", value = "当前页") @RequestParam("pageNum") Long current,
                                                       @ApiParam(name = "limit", value = "分页大小") @RequestParam("limit") Long size,
                                                       @RequestParam Map<String, Object> param){
        Page<OrgPositionPageVO> page = new Page(current, size);
        return orgPositionService.getorgPositionType(page,param);
    }

    /**
     * 查询角色菜单数据范围权限  复选框组
     *
     * @param menuId
     * @param positionIds
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @author renwd
     * @since 2019/7/25
     */
    @GetMapping("/positionMenuRange/{menuId}/{positionIds}")
    public List<Map<String, Object>> rangeRemoteUrl2(@PathVariable(value = "menuId") Long menuId, @PathVariable(value = "positionIds") String positionIds) {
        Integer isFixed = roleService.isFixed();
        Integer permissionType = ManageDictCodeConstants.BILL_TYPE_PERMISSION_POSITION;
        if (StringUtils.isEquals(ManageDictCodeConstants.STATUS_IS_YES, isFixed.toString())) {
            return roleMenuRangeService.remoteSoftUrl(menuId, permissionType, positionIds);

        } else {
            return roleMenuRangeService.remoteUrl(menuId);
        }
    }

    /**
     * 查询职位菜单操作按钮 复选框组
     *
     * @param menuId
     * @param positionIds
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @author renwd
     * @since 2019/7/25
     */
    @GetMapping("/positionMenuAction/{menuId}/{positionIds}")
    public List<Map<String, Object>> actionRemoteUrl(@PathVariable(value = "menuId") Long menuId, @PathVariable(value = "positionIds") String positionIds) {
        Integer isFixed = roleService.isFixed();
        Integer permissionType = ManageDictCodeConstants.BILL_TYPE_PERMISSION_POSITION;
        if (StringUtils.isEquals(ManageDictCodeConstants.STATUS_IS_YES, isFixed.toString())) {
            return roleMenuActionService.remoteSoftUrl(menuId, permissionType, positionIds);
        } else {
            return roleMenuActionService.remoteUrl(menuId);
        }
    }

    /**
     * 加载职位权限信息
     *
     * @param id
     * @return java.lang.String
     * @author dengqiang
     * @since 2019/7/25
     */
    @GetMapping(value = "/positionData/{id}")
    @ResponseBody
    public Map<String,Object>  roleData(@PathVariable(value = "id")String  id){
        //根据职位id去找到相对应的角色id
        String ids = permissionService.getRoleIds(id);

        logger.debug(">>>>>>>>>>>>>>>>>ids============================:{}",ids);

        Map<String, String> checkbox = permissionCtrlService.queryMenuCtrl(ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE,ids);//you
        List<CommonTreeDTO> orgList = permissionService.queryPermissionMenu(ids,ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE);
        logger.debug(">>>>>>>>>>>>>>>>> orgList============================:{}", orgList);
        Map<String,Object> roleData=new HashMap<>();
        roleData.put("treejson", orgList);
        roleData.put("checkbox", checkbox);
        roleData.put("treeMenuAction", permissionMenuActionService.findMenuAction(ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE,ids));
        roleData.put("treeMenuRange", permissionMenuRangeService.findMenuRange(ManageDictCodeConstants.BILL_TYPE_PERMISSION_ROLE,ids));
        roleData.put("isFixed", 10041001);
        logger.debug(">>>>>>>>>>>>>>>>> roleData============================:{}", roleData);

        return roleData;

    }


    /**
     * 根据id修改职位信息OrgPositionPcDTO
     *
     * @param id
     * @param ptdto
     * @return void
     * @author dengqiang
     * @since 2019/8/5
     */
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    public void modifyPosition(@PathVariable(value = "id") Long id, @RequestBody OrgPositionPcDTO ptdto) {
        orgPositionService.modifyOrgPosition(id, ptdto);
    }
}
